﻿Public Class ConverteNHQL

#Region "CONSTRUTOR(S)"

    Public Sub New()
        MyBase.New()
    End Sub


#End Region

#Region "CONSTANTES"

    Public Const NHQL_MAIOR As String = " > "

    Public Const NHQL_MENOR As String = " < "

    Public Const NHQL_MAIOR_OU_IGUAL As String = " >= "

    Public Const NHQL_MENOR_OU_IGUAL As String = " <= "

    Public Const NHQL_DIFERENTE As String = " != "

    Public Const NHQL_IGUAL As String = " = "

    Public Const NHQL_NULO As String = " is null"

    Public Const NHQL_CONTEM As String = " like "

#End Region

#Region "METODOS DA CLASSE"

    Public Function Converte(ByVal condicoes As IList(Of Condicao), ByVal nomeCompleto As String, ByVal nome As String) As String

        Dim NHQL As String = "from " & nomeCompleto & " as " & nome.ToLower

        Dim condicao As String = CondicoesNHQL(condicoes, nome)

        If Not condicao.Equals("") Then

            NHQL += " where " + condicao

        End If

        Return NHQL

    End Function

    Private Function CondicoesNHQL(ByVal condicoes As IList(Of Condicao), ByVal nome As String) As String

        Dim NHQL As String = ""

        For Each condicao As Condicao In condicoes

            If condicao.Operador.Equals(condicao.IGUAL) Then

                Dim valor As String = Me.FormataValor(condicao.Valor, condicao.Tipo)
                NHQL += String.Concat(condicao.Campo, NHQL_IGUAL, valor)

            ElseIf condicao.Operador.Equals(condicao.MAIOR) Then

                Dim valor As String = Me.FormataValor(condicao.Valor, condicao.Tipo)
                NHQL += String.Concat(condicao.Campo, NHQL_MAIOR, valor)

            ElseIf condicao.Operador.Equals(condicao.MAIOR_OU_IGUAL) Then

                Dim valor As String = Me.FormataValor(condicao.Valor, condicao.Tipo)
                NHQL += String.Concat(condicao.Campo, NHQL_MAIOR_OU_IGUAL, valor)

            ElseIf condicao.Operador.Equals(condicao.MENOR) Then

                Dim valor As String = Me.FormataValor(condicao.Valor, condicao.Tipo)
                NHQL += String.Concat(condicao.Campo, NHQL_MENOR, valor)

            ElseIf condicao.Operador.Equals(condicao.MENOR_OU_IGUAL) Then

                Dim valor As String = Me.FormataValor(condicao.Valor, condicao.Tipo)
                NHQL += String.Concat(condicao.Campo, NHQL_MENOR_OU_IGUAL, valor)

            ElseIf condicao.Operador.Equals(condicao.NULO) Then

                NHQL += String.Concat(condicao.Campo, NHQL_NULO, condicao.Valor)

            ElseIf condicao.Operador.Equals(condicao.CONTEM) Then

                Dim valor As String = "'%" & condicao.Valor & "%'"
                NHQL += String.Concat(condicao.Campo, NHQL_CONTEM, valor)

            End If

            If (condicoes.IndexOf(condicao) < (condicoes.Count - 1)) Then
                NHQL += " and "
            End If

        Next

        Return NHQL

    End Function

    Private Function FormataValor(ByVal valor As String, ByVal tipo As String) As String

        If (tipo.Equals("") Or tipo.Equals(Condicao.TEXTO)) Then

            Return "'" & valor & "'"

        End If

        Return valor

    End Function

#End Region

End Class
